*********************************************************************************
** Supplement to TableA5.do
** 
*******************************************************************************

clear all 
set more off 



**
** file settings 
**
* survival and readmission data 
local fpath_survreadm_dat = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/survreadm/input"

* crosswalks
local fpath_crosswalks = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/build/crosswalks"

* build folder 
local fpath_build_output = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/build/output"

* output directory 
local fpath_output = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/output_20230606/"

** which sections of the code to run?
local prepare_data = 1 
local run_regressions = 1
local clean_es_table = 0 







**
** prepare the data 
**
**
if `prepare_data' == 1 {
** read in the data
insheet using "`fpath_survreadm_dat'/rev_stackedv2_20230606.csv", clear 


**
** drop immediately divested hospitals from the control group 
preserve 
import excel "`fpath_crosswalks'/Acq AHA Xwalk (Dalton).xlsx", sheet("combined (stata input)") firstrow clear
keep if (divested_yr <= 2008) | (divested_yr == 2015 & target2 == 1)
rename id_aha_dalton id
keep id 
tempfile drop_divested
save `drop_divested', replace
restore 

cap drop _merge 
merge m:1 id using `drop_divested', keep(master) nogen



* collapse to hosp-yeargroup-measure level
* the yeargroups are 2001-2003, 2004-2006, 2008-2010, 2012-2014
gen acq_legacy = (ind12_14acq_legacy > 0) | (ind08_11acq_legacy > 0) | (ind04_06acq_legacy > 0) | (ind01_03acq_legacy> 0 )
gen target = (ind12_14target > 0) | (ind08_11target > 0) | (ind04_06target > 0) | (ind01_03target > 0)
gen target2 = (ind12_14target2 > 0) | (ind08_11target2 > 0) | (ind04_06target2 > 0) | (ind01_03target2 > 0)
gen acq_other = (ind12_14acq_other > 0) | (ind08_11acq_other > 0) | (ind04_06acq_other > 0) | (ind01_03acq_other> 0)

keep if inlist(year, 2003, 2006, 2010, 2014)

*  restrict the sample: legacy, target, other for-profit  
keep if forprofit == 1 
drop if target2 == 1 | acq_other == 1 

label var ind08_11acq_legacy "Acquirer * 08-11"
label var ind12_14target "Target * 12-14"

* define post-treatment 
gen postm = ( year >= 2008)
gen postm_target = postm * target
gen postm_legacy = postm * acq_legacy 
label var postm_target "Post 2008 * Target"
label var postm_legacy "Post 2008 * Acquirer"


* for the event study: create  year * post indicators
foreach hosp in acq_legacy target {
	
	local hosptitle = ustrtitle("`hosp'")
	
	foreach year in 2003 2006 2010 2014 {
		
		gen year`year'x`hosp' = (year == `year') * `hosp'
	
		
		label var year`year'x`hosp' "`year' x `hosptitle'"
		
	}
}



* sanity check... 
assert target2 + acq_other == 0 
assert acq_legacy + target <= 1

* run DD as before but with hospital-measure FEs, yeargroup-measure FEs 
* gen hospital-measure FEs
egen hospmeasure = group(id cohort)

*gen year-measure FEs
egen yrmeasure = group(year cohort)

* gen HRR-yeargroup-measure FEs
egen hrryrmeasure = group(hrrcode year cohort)

* gen HRR year time TREND by cohort 
gen hrr_time_ami = hrrcode * year * (cohort == "ami")
gen hrr_time_chf = hrrcode * year * (cohort == "chf")
gen hrr_time_hip = hrrcode * year * (cohort == "hip")
gen hrr_time_pnu = hrrcode * year * (cohort == "pnu")
gen hrr_time_stk = hrrcode * year * (cohort == "stk")

* interact log(beds) with measure
tab cohort, gen(cr)
foreach v of varlist cr* {
	gen lbds`v' = log(hospbd) * `v'
	local lab: variable label `v'
	local lab = subinstr("`lab'", "cohort==", "log(beds) * ", 1)
	label var lbds`v' "`lab'"
}
egen id2 = group(id)

* create variable labels
label var rhdx "Risk-Adjusted Readmission Rate"
label var shdx "Risk-Adjusted Survival Rate"
label var rnra "Unadjusted Readmission Rate"
label var snra "Unadjusted Survival Rate"
}






*****
***** run the regressions 
*****
*****
if `run_regressions' ==  1 {
	foreach v in shdx rhdx snra rnra {


	************************************************************************
	* survival and readmission regression - main
	************************************************************************
		reghdfe `v' postm_legacy postm_target if forprofit == 1, absorb(hospmeasure yrmeasure) vce(cluster id2)

		regsave using "`fpath_output'/`v'_main_tablea5", p autoid addlabel(rhs, "`v'", spec, "main") replace 

	
	
	************************************************************************
	* survival and readmission regression - event study 
	************************************************************************
		reghdfe `v' year2003* year2010* year2014* if forprofit == 1, absorb(hospmeasure yrmeasure) vce(cluster id2)

		local rhs_lab: variable label `v'
	
		regsave using "`fpath_output'/`v'_es_main_tablea5", p autoid addlabel(rhs, "`v'", spec, "main", rhs_lab, "`rhs_lab'") replace ci level(95)
	
	
	} 
}	
	
	
	
	
	
	
	
*****
***** clean the event study table 
*****
*****
if `clean_es_table' == 1{
	
	clear
	save "`fpath_output'/survreadm_es_tables_comb.dta", emptyok replace 

	foreach v in shdx rhdx snra rnra {
	 
		clear 

		di"`v'" 
		use "`fpath_output'/`v'_es_main", clear 
	
		append using "`fpath_output'/survreadm_es_tables_comb.dta"
		
		save "`fpath_output'/survreadm_es_tables_comb.dta", replace 		
	
	}

	use "`fpath_output'/survreadm_es_tables_comb.dta", clear
	drop if strpos(var, "_cons") | strpos(var, "interim") | strpos(var, "hrr")

	gen year = subinstr(subinstr(subinstr(var, "year", "", 1), "xlegacy", "", 1), "xtarget", "", 1)
	gen system = "Acquirer" if strpos(var, "legacy")
	replace system = "Target" if strpos(var, "target")
	

	cap drop _id
	cap drop r2 spec 
	drop rhs var ci*
	foreach v in coef stderr pval {
	replace `v' = round(`v', .000001)
	}
	
	gen asterisk = "***" if pval <= .01
	replace asterisk = "**" if pval > .01 & pval <= .05
	replace asterisk = "*" if pval >.05 & pval < .1
	gen paren1 = "("
	gen paren2 = ")"
	
	egen coef2 = concat(coef asterisk)
	egen stderr2 = concat(paren1 stderr paren2)

	drop coef stderr paren1 paren2 asterisk pval
	rename coef2 coef
	rename stderr2 stderr 
	
	egen rhs_code = group(rhs_lab)
	
	gen paste = "x"
	egen var = concat(system paste year)
	cap drop paste system year rhs_lab
	
	
	reshape wide coef stderr , i(var N) j(rhs_code)
	order N, last 


	rename coef1 Adj_read_coef
	rename coef2 Adj_surv_coef
	rename coef3 Unadj_read_coef
	rename coef4 Unadj_surv_coef 

	rename stderr1 Adj_read_se 
	rename stderr2 Adj_surv_se
	rename stderr3 Unadj_read_se
	rename stderr4 Unadj_surv_se
	
	label var Adj_read_coef "Adjusted readmission coef"
	label var Adj_surv_coef "Adjusted survival coef"
	label var Unadj_read_coef "Unadjusted readmission coef"
	label var Unadj_surv_coef "Unadjusted survival coef"
	
	label var Adj_read_se "Adjusted readmission se"
	label var Adj_surv_se "Adjusted survival se"
	label var Unadj_read_se "Unadjusted readmission se"
	label var Unadj_surv_se "Unadjusted survival se"
	
	replace var = subinstr(var, "x", " x ", 1)

	*export excel using "`fpath_output'/REStat_revision_tables.xls", sheet("Event studies survreadm", replace) firstrow(varlabel)
}





